package org.budo.dubbo.protocol.http.dispatcher.servlet;

import java.io.InputStream;

import javax.servlet.http.HttpServletResponse;

import org.budo.dubbo.protocol.http.invocation.adapter.ProtocolResponse;
import org.budo.support.lang.util.IoUtil;

/**
 * @author lmw
 */
public class ServletProtocolResponse implements ProtocolResponse {
    private HttpServletResponse response;

    public ServletProtocolResponse() {}

    public ServletProtocolResponse(HttpServletResponse response) {
        this.response = response;
    }

    public HttpServletResponse getResponse() {
        return this.response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    @Override
    public void addHeader(String name, String value) {
        response.addHeader(name, value);
    }

    @Override
    public void write(InputStream inputStream) {
        try {
            IoUtil.write(inputStream, response.getOutputStream());
            response.flushBuffer();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}